package com.mock.ems.schedule;

import com.mock.base.util.DateUtil;
import com.mock.ems.config.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class UserManagerRemoveSchduler {

    private static final Logger logger = LoggerFactory.getLogger(UserManagerRemoveSchduler.class);

    private static Long clearTime = 30 * 60L;//半小时清空

    public static void schedule() {

        logger.info("secKill scheduler start.");
        Executors.newSingleThreadScheduledExecutor()
                .scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Long nowTime = DateUtil.toSeconds(DateUtil.getDate().getTime());
                            UserManager.getUserMap().forEach((k,v) -> {
                                Long createTime = v.getLong("expiredTime");
                                if(nowTime - createTime >= clearTime){
                                    UserManager.remove(k);
                                }
                            });
                        }catch (Exception e) {
                            logger.error(e.getLocalizedMessage());
                        }
                    }
                },0,60, TimeUnit.SECONDS);
    }
}
